package br.com.farmabid.component;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import br.com.caelum.vraptor.AroundCall;
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.interceptor.SimpleInterceptorStack;
import br.com.farmabid.controller.HomeController;

@Intercepts
@RequestScoped
public class AcessInterceptor {
	 @Inject private Result result;
	 @Inject private AcessSession session;
	 @Inject private ControllerMethod method;
	 @Inject protected ResourceBundle bundle;
	 private List<String> errors;
	 
	@AroundCall
	public void intercept(SimpleInterceptorStack stack) throws InterceptionException {
		if((session == null || session.getNome() == null)
			&& !method.getController().getType().getName().contains("Home")
			&& !method.getController().getType().getName().contains("Cadastro")
			&& !method.getMethod().getName().contains("entrar")
			&& !method.getController().getType().getName().contains("ProdutoController")
			&& !method.getController().getType().getName().contains("ProdutoWS")
			
			&& !method.getMethod().getName().contains("enviarNovaSenha")){
			errors = new ArrayList<String>();
			errors.add(bundle.getString("alert.login.necessario"));
			result.redirectTo(HomeController.class).index();
		} else {
			stack.next();
		}
	}

}